package com.example.demo.sevice.imp;

import com.example.demo.controller.data.ReturnData;
import com.example.demo.dao.CircleDao;
import com.example.demo.dao.CircleLikeDao;
import com.example.demo.dao.CommentDao;
import com.example.demo.dao.UserDao;
import com.example.demo.entity.Circle;
import com.example.demo.entity.CircleLike;
import com.example.demo.entity.User;
import com.example.demo.sevice.CircleService;
import com.example.demo.sevice.CommentService;
import com.example.demo.util.TokenUtil;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 圈子相关的实现类
 */
@Service
public class CircleServiceImp implements CircleService {
    @Resource
    CircleDao circleDao;
    @Resource
    UserDao userDao;
    @Resource
    CircleLikeDao circleLikeDao;
    @Resource
    CommentDao commentDao;
    /**
     * 发布圈子的实现方法
     *
     * @param circle
     * @param token
     * @return
     */
    @Override
    public ReturnData add(Circle circle, String token) {

        User user  =  new User();
        String username = TokenUtil.getUsernameByToken(token);
        if(username.equals("")){
            return ReturnData.error("没有这个用户");
        }
        user = userDao.findByUsername(username).get(0);
        if (StringUtils.isEmpty(circle.getContent()) &&
                circle.getImages().equals("[]") &&
                StringUtils.isEmpty(circle.getVideo())) {//如果三个同时为空
            return ReturnData.error("发布的内容为空");
        }
        circle.setUserId(user.getId());
        circle.setTime(new Timestamp(System.currentTimeMillis()));
        circleDao.save(circle);
        return ReturnData.ok("发布成功");
    }

    /**
     * @param page     页码
     * @param pageSize 每一页的长度 默认为10
     * @param token
     * @return
     */
    @Override
    public ReturnData getList(int page, int pageSize, String token) {
        String username = TokenUtil.getUsernameByToken(token);
        User user = userDao.findByUsername(username).get(0);
        List<Map<String, Object>> list = circleDao.getList((page - 1) * pageSize, pageSize,user.getId());
        List<HashMap<String ,Object>> mapList = new ArrayList<>();
        for (Map<String, Object> map :list){//遍历
           HashMap<String,Object> temp = new HashMap<>(map);
            List comments = commentDao.getComment((Integer) map.get("id"));
            temp.put("comments",comments);
            mapList.add(temp);
        }

        long count = circleDao.count();//表里面有多少条数据
        long num;
        if (count % pageSize == 0) {
            num = count / pageSize;
        } else {
            num = count / pageSize + 1;
        }
        HashMap map = new HashMap();
        map.put("list", mapList);//当前页码的数据
        map.put("total", num);//总页码数据
        return ReturnData.ok(map);
    }

    /**
     * 获取当前登录用户发布的内容
     * @param page 页码，1 2 3 4
     * @param pageSize 每一页的大小 20
     * @param token 登录用户的token
     * @return
     */
    @Override
    public ReturnData myList(int page, int pageSize, String token) {

        String username = TokenUtil.getUsernameByToken(token);
        User user = userDao.findByUsername(username).get(0);
        List<Map<String, Object>> list = circleDao.myList((page - 1) * pageSize, pageSize, user.getId());

        long count = circleDao.findByUserId(user.getId()).size();//表里面有多少条数据

        long num;
        if (count % pageSize == 0) {
            num = count / pageSize;
        } else {
            num = count / pageSize + 1;
        }
        HashMap map = new HashMap();
        map.put("list", list);//当前页码的数据
        map.put("total", num);//总页码数据
        return ReturnData.ok(map);
    }
    /**
     * 点赞或者取消点赞接口
     * @param circleLike
     * @param token
     * @return
     */
    @Override
    public ReturnData like(CircleLike circleLike, String token) {
        User user  =  new User();
        String username = TokenUtil.getUsernameByToken(token);
        if(username.equals("")){
            return ReturnData.error("没有这个用户");
        }
        user = userDao.findByUsername(username).get(0);
        circleLike.setUserId(user.getId());
        List<CircleLike> list = circleLikeDao.findByUserIdAndCircleId(user.getId(), circleLike.getCircleId());
        circleLike.setTime(new Timestamp(System.currentTimeMillis()));
        if(list.size()>0){//修改
            circleLike.setId(list.get(0).getId());
            circleLikeDao.save(circleLike);//更新表
        }else{
            circleLikeDao.save(circleLike);//插入表
        }
        if(circleLike.getStatus()==1){
            return ReturnData.ok("点赞成功");
        }else{
            return ReturnData.ok("取消点赞成功");
        }

    }

    @Override
    public ReturnData getLikeUsers(int circleId) {
        return ReturnData.ok(circleLikeDao.getLikeUsers(circleId));
    }

    @Override
    public ReturnData myLikeList(int page, int pageSize, String token) {
        String username = TokenUtil.getUsernameByToken(token);
        User user = userDao.findByUsername(username).get(0);
        //查找对应的数据
        List<Map<String, Object>> list = circleDao.myLikeList((page - 1) * pageSize, pageSize, user.getId());

        long count = circleLikeDao.getLikeCount(user.getId());//表里面有多少条数据

        long num;
        if (count % pageSize == 0) {
            num = count / pageSize;
        } else {
            num = count / pageSize + 1;
        }
        HashMap map = new HashMap();
        map.put("list", list);//当前页码的数据
        map.put("total", num);//总页码数据
        return ReturnData.ok(map);
    }

    /**
     * 删除指定ID的圈子
     * @param id 圈子ID
     * @param token 当前登录用户的token
     * @return
     */
    @Override
    public ReturnData delete(int id, String token) {
        //获取登录用户信息
        String username = TokenUtil.getUsernameByToken(token);
        User user = userDao.findByUsername(username).get(0);
        //根据ID获取圈子数据
        Circle circle = circleDao.findById(id).get();
        if(circle.getUserId()!=user.getId()){//如果当前的用户ID 和圈子里的发布者ID不一样
            return ReturnData.error("无权限删除");
        }
        circleDao.delete(circle);
        return  ReturnData.ok("删除成功");
    }
}
